package com.rlyy.basic.resolver.argument;

import com.rlyy.common.param.Request;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Component;

import java.lang.reflect.Parameter;
import java.util.Optional;

/**
 * 简单数据类型解析  基本数据类型 + String
 *
 * @author yuanqinglong
 * @since 2020/7/22 9:45
 */
@Component
public class SimplePropertyMethodArgumentResolver implements MyHandlerMethodArgumentResolver {


    @Override
    public boolean supportsParameter(Parameter parameter) {
        return BeanUtils.isSimpleProperty(parameter.getType());
    }

    @Override
    public Object resolveArgument(Parameter parameter, Request fullRequest) throws Exception {
        return Optional.ofNullable(fullRequest.getData())
                .map(d-> d.getObject(parameter.getName(), parameter.getType()))
                .orElse(null);
    }

}
